<?php
namespace Admin\Model;
/**
 * 商品服务类
 */
class GroupModel extends BaseModel {

    /**
     * 获取商品信息
     */
    public function get(){
        $m = M('goods');
        $id = (int)I('id',0);
        $goods = $m->where("goodsId=".$id)->find();
        //相册
        $m = M('goods_gallerys');
        $goods['gallery'] = $m->where('goodsId='.$id)->select();
        //属性
        if($goods['attrCatId']>0){
            $sql = "select catName from __PREFIX__attribute_cats where catId=".$goods['attrCatId'];
            $rs = $this->query($sql);
            $goods['attrCatName'] = $rs[0]['catName'];

            //获取规格属性
            $sql = "select ga.attrVal,ga.attrPrice,ga.attrStock,a.attrId,a.attrName,a.isPriceAttr,ga.isRecomm
			            ,ga.isRecomm from __PREFIX__attributes a
			            left join __PREFIX__goods_attributes ga on ga.attrId=a.attrId and ga.goodsId=".$id." where
						a.attrFlag=1 and a.catId=".$goods['attrCatId']." and a.shopId=".$goods['shopId'];
            $sql .=" order by ga.id asc";
            $attrRs = $m->query($sql);
            if(!empty($attrRs)){
                $priceAttr = array();
                $attrs = array();
                foreach ($attrRs as $key =>$v){
                    if($v['isPriceAttr']==1){
                        $goods['priceAttrName'] = $v['attrName'];
                        $priceAttr[] = $v;
                    }else{
                        $v['attrContent'] = $v['attrVal'];
                        $attrs[] = $v;
                    }
                }
                $goods['priceAttrs'] = $priceAttr;
                $goods['attrs'] = $attrs;
            }
        }
        $goodsGroupModel = M('GoodsGroup');
        $group = $goodsGroupModel->where('goodsId ='.$goods['goodsId'])->find();
        return array_merge($goods,$group);
    }
    /**
     * 分页列表[获取待审核列表]
     */
    public function queryPenddingByPage(){
        $m = M('goods');
        $shopName = I('shopName');
        $goodsName = I('goodsName');
        $groupStatus = (int)I('groupStatus',-1);
        $areaId1 = (int)I('areaId1',0);
        $areaId2 = (int)I('areaId2',0);
        $goodsCatId1 = (int)I('goodsCatId1',0);
        $goodsCatId2 = (int)I('goodsCatId2',0);
        $goodsCatId3 = (int)I('goodsCatId3',0);
        $sql = "select g.*,gc.catName,sc.catName shopCatName,p.shopName,gp.id,gp.goodsGroupStatus,gp.groupStatus,gp.groupPrice,gp.groupMaxCount,gp.groupMinCount,gp.endTime,gp.startTime from __PREFIX__goods g
              left join __PREFIX__goods_cats gc on g.goodsCatId3=gc.catId
              left join __PREFIX__shops_cats sc on sc.catId=g.shopCatId2,__PREFIX__shops p,__PREFIX__goods_group gp where p.shopId=g.shopId and g.goodsId=gp.goodsId and  g.isGroup = 1 and g.goodsFlag = 1 and g.isSale=1 and gp.goodsGroupStatus=0";
        if($areaId1>0)$sql.=" and p.areaId1=".$areaId1;
        if($areaId2>0)$sql.=" and p.areaId2=".$areaId2;
        if($goodsCatId1>0)$sql.=" and g.goodsCatId1=".$goodsCatId1;
        if($goodsCatId2>0)$sql.=" and g.goodsCatId2=".$goodsCatId2;
        if($goodsCatId3>0)$sql.=" and g.goodsCatId3=".$goodsCatId3;
        if($shopName!='')$sql.=" and (p.shopName like '%".$shopName."%' or p.shopSn like '%".$shopName."%')";
        if($goodsName!='')$sql.=" and (g.goodsName like '%".$goodsName."%' or g.goodsSn like '%".$goodsName."%')";
        if($groupStatus>=0)$sql.=" and gp.groupStatus =".$groupStatus;
        $sql.="  order by gp.id desc";
        $goods = $m->pageQuery($sql);
        //判断团购商品状态
        foreach($goods['root'] as $k=>$v){
            $goods['root'][$k]['startTime'] = date('Y-m-d H:i:s',$v['startTime']);
            $goods['root'][$k]['endTime'] = date('Y-m-d H:i:s',$v['endTime']);
            //团购订单商品信息,获取团购数量
            $ordersGoodsModel = M('OrderGoods');
            $orderGoods = $ordersGoodsModel->where('goodsGroupId = '.$v['id'])->select();
            $totalNums = 0;
            foreach($orderGoods as $ks=>$vs){
                $totalNums += $vs['goodsNums'];
            }
            $goods['root'][$k]['totalNums'] = $totalNums;     
        }
        return $goods;
    }
    /**
     * 分页列表[获取已审核列表]
     */
    public function queryByPage(){
        $m = M('goods');
        $shopName = I('shopName');
        $goodsName = I('goodsName');
        $groupStatus = (int)I('groupStatus',-1);
        $areaId1 = (int)I('areaId1',0);
        $areaId2 = (int)I('areaId2',0);
        $goodsCatId1 = (int)I('goodsCatId1',0);
        $goodsCatId2 = (int)I('goodsCatId2',0);
        $goodsCatId3 = (int)I('goodsCatId3',0);
        $isAdminBest = (int)I('isAdminBest',-1);
        $isAdminRecom = (int)I('isAdminRecom',-1);
        $sql = "select g.*,gc.catName,sc.catName shopCatName,p.shopName,gp.id,gp.goodsGroupStatus,gp.groupStatus,gp.groupPrice,gp.groupMaxCount,gp.groupMinCount,gp.endTime,gp.startTime from __PREFIX__goods g
	 	      left join __PREFIX__goods_cats gc on g.goodsCatId3=gc.catId
	 	      left join __PREFIX__shops_cats sc on sc.catId=g.shopCatId2,__PREFIX__shops p,__PREFIX__goods_group gp where p.shopId=g.shopId and g.goodsId=gp.goodsId and  g.isGroup = 1 and g.goodsFlag = 1 and g.isSale=1 and gp.goodsGroupStatus=1";
        if($areaId1>0)$sql.=" and p.areaId1=".$areaId1;
        if($areaId2>0)$sql.=" and p.areaId2=".$areaId2;
        if($goodsCatId1>0)$sql.=" and g.goodsCatId1=".$goodsCatId1;
        if($goodsCatId2>0)$sql.=" and g.goodsCatId2=".$goodsCatId2;
        if($goodsCatId3>0)$sql.=" and g.goodsCatId3=".$goodsCatId3;
        if($isAdminBest>=0)$sql.=" and g.isAdminBest=".$isAdminBest;
        if($isAdminRecom>=0)$sql.=" and g.isAdminRecom=".$isAdminRecom;
        if($shopName!='')$sql.=" and (p.shopName like '%".$shopName."%' or p.shopSn like '%".$shopName."%')";
        if($goodsName!='')$sql.=" and (g.goodsName like '%".$goodsName."%')";
        if($groupStatus>=0)$sql.=" and gp.groupStatus =".$groupStatus;
        $sql.="  order by gp.id desc";
        $goods = $m->pageQuery($sql);
        //判断团购商品状态
        foreach($goods['root'] as $k=>$v){
            $goods['root'][$k]['startTime'] = date('Y-m-d H:i:s',$v['startTime']);
            $goods['root'][$k]['endTime'] = date('Y-m-d H:i:s',$v['endTime']);
            //团购订单商品信息,获取团购数量
            $sql = "select og.* from __PREFIX__order_goods og left join __PREFIX__orders o on o.orderId=og.orderId where o.orderStatus in(0,1,2,3,4) and og.goodsGroupId = ".$v['id'];
            $orderGoods = $this->query($sql);
            $totalNums = 0;
            foreach($orderGoods as $ks=>$vs){
                $totalNums += $vs['goodsNums'];
            }
            $goods['root'][$k]['totalNums'] = $totalNums;     
        }
        return $goods;
    }
    /**
     * 获取列表
     */
    public function queryByList(){
        $m = M('goods');
        $sql = "select * from __PREFIX__goods order by goodsId desc";
        return $m->find($sql);
    }
    /**
     * 修改商品状态
     */
    public function changeGoodsStatus(){
        $rd = array('status'=>-1);
        $m = M('GoodsGroup');
        $id = (int)I('id',0);
        $data['goodsGroupStatus'] = (int)I('status',0);
        $rs = $m->where('id='.$id)->save($data);
        if(false !== $rs){
            $sql = "select goodsName,userId from __PREFIX__goods g,__PREFIX__shops s where g.shopId=s.shopId and g.goodsId=".$id;
            $goods = $this->query($sql);
            $msg = "";
            if(I('status',0)==0){
                $msg = "商品[".$goods[0]['goodsName']."]已被商城下架";
            }else{
                $msg = "商品[".$goods[0]['goodsName']."]已通过审核";
            }
            $yj_data = array(
                'msgType' => 0,
                'sendUserId' => session('WST_STAFF.staffId'),
                'receiveUserId' => $goods[0]['userId'],
                'msgContent' => $msg,
                'createTime' => date('Y-m-d H:i:s'),
                'msgStatus' => 0,
                'msgFlag' => 1,
            );
            M('messages')->add($yj_data);
            $rd['status'] = 1;
        }
        return $rd;
    }
    /**
     * 获取待审核的团购活动数量
     */
    public function queryPenddingGroupNum(){
        $rd = array('status'=>-1);
        $m = M('goods');
        $sql="select count(*) counts from __PREFIX__goods g
              left join __PREFIX__goods_cats gc on g.goodsCatId3=gc.catId
              left join __PREFIX__shops_cats sc on sc.catId=g.shopCatId2,__PREFIX__shops p,__PREFIX__goods_group gp where p.shopId=g.shopId and g.goodsId=gp.goodsId and  g.isGroup = 1 and g.goodsFlag = 1 and g.isSale=1 and gp.goodsGroupStatus=0";
        $rs = $this->query($sql);
        $rd['num'] = $rs[0]['counts'];
        return $rd;
    }



};
?>